package com.money.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.money.dao.OrderDao;
import com.money.entity.CbbUserEntity;
import com.money.entity.CddRechargeDetailsEntity;
import com.money.service.CbbUserService;
import com.money.service.OrderService;
import com.money.utile.PayUtile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DecimalFormat;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderDao, CddRechargeDetailsEntity> implements OrderService {

    @Autowired
    private CbbUserService userService;

    @Transactional
    @Override
    public void queryStatus(String orderId) {
        QueryWrapper<CddRechargeDetailsEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("rd_uuid", orderId);
        CddRechargeDetailsEntity orderEntity = getOne(wrapper);
        //没有支付我们就取消这个支付订单 也就是我们来改变充值订单的状态
        int status = orderEntity.getRdStatus() == 1 ? 0 : 2;
        //当前订单状态已经是支付过后了我们就不允许在去修改我们的订单状态了
        if (status == 2) return;
        Double aDouble1 = PayUtile.getPaymentSuccess(orderId);
        if (aDouble1.equals(orderEntity.getRdPrice())) {
            //给用户添加钱包的余额
            Long urId = orderEntity.getRdUrId();
            CbbUserEntity user = userService.getById(urId);
            double wallet = user.getUrWallet();
            double he = wallet + orderEntity.getRdPrice();
            DecimalFormat df = new DecimalFormat("#0.00");
            double aDouble = Double.parseDouble(df.format(he));
            user.setUrWallet(aDouble);
            orderEntity.setRdCountPrice(aDouble);
            userService.updateById(user);
        }
        orderEntity.setRdStatus(status);
        updateById(orderEntity);
    }
}
